package phaseutil

import (
	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
	"sigs.k8s.io/cluster-api/util/conditions"

	bkev1beta1 "gopkg.openfuyao.cn/cluster-api-provider-bke/api/v1beta1"
)

func AllowDeployAddon(bkeCluster *bkev1beta1.BKECluster, cluster *clusterv1.Cluster) bool {
	// 如果master节点未被初始化，不允许部署addon
	if !conditions.IsTrue(cluster, clusterv1.ControlPlaneInitializedCondition) {
		return false
	}
	// 集群中有一主一从点了就允许部署addon了
	statusWorkerNodes := GetBKENodesFromNodesStatus(bkeCluster.Status.NodesStatus).Worker()
	for _, statusWorkerNode := range statusWorkerNodes {
		if bkeCluster.GetNodeStateFlag(statusWorkerNode.IP, bkev1beta1.NodeBootFlag) {
			return true
		}
	}

	// 集群中有一master/worker 节点了就允许部署addon了
	statusMasterWorkerNodes := GetBKENodesFromNodesStatus(bkeCluster.Status.NodesStatus).MasterWorker()
	for _, statusMasterWorkerNode := range statusMasterWorkerNodes {
		if bkeCluster.GetNodeStateFlag(statusMasterWorkerNode.IP, bkev1beta1.NodeBootFlag) {
			return true
		}
	}

	// 全部节点都被引导才允许
	//expectBootNodeNum := len(bkeCluster.Spec.ClusterConfig.Nodes)
	//failedBootNodeNum, successBootNodeNum := CalculateBKEMachineBootNum(bkeMachines)
	//// 如果成功的加失败的数量不为spec中的节点数量，说明还有节点没有进行引导，不允许部署addon
	//if successBootNodeNum+failedBootNodeNum != expectBootNodeNum {
	//	return false
	//}
	return false
}
